package com.caissa.springboot.starter.leaf.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caissa.framework.starter.autoconfigure.annotation.CatAop;
import com.base.springboot.starter.leaf.config.LeafProperties;
import com.base.springboot.starter.leaf.domain.EcpCommonException;
import com.base.springboot.starter.leaf.domain.LeafCodeEnum;
import com.base.springboot.starter.leaf.domain.MethodEnum;
import com.base.springboot.starter.leaf.service.LeafService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author：zhangqiang
 * @Date 2020/8/21 10:56
 */
@Service
public class LeafServiceImpl implements LeafService {
    /**
     * 默认业务类型
     */
    private String defaultBizType = "defaultBizType";

    private int timeTime = 10000;

    @Autowired
    private LeafProperties properties;

    /**
     * 获取分布式id
     *
     * @return
     */
    @CatAop
    @Override
    public String getSnowflakeID() {
        String url = getUrl(properties.getPrefix(), MethodEnum.SNOW_FLAKE_GET);
        String id = HttpUtil.get(url, timeTime);
        if (StringUtils.isEmpty(id)) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_ID_FAIL);
        }
        return id;
    }

    /**
     * 批量获取分布式id
     *
     * @param num 数量
     * @return
     */
    @CatAop
    @Override
    public List<String> getSnowflakeID(int num) {
        String url = getUrl(properties.getPrefix(), MethodEnum.SNOW_FLAKE_BATCH_GET);
        if (num < 1 || num > 10000) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_NUM_FAIL);
        }
        url = url + "?num=" + num;
        String idsJson = HttpUtil.get(url, timeTime);
        JSONArray jsonArray = JSONObject.parseArray(idsJson);
        if(jsonArray == null){
            throw new EcpCommonException(LeafCodeEnum.PARAMS_ID_FAIL);
        }
        List<String> idList = jsonArray.toJavaList(String.class);
        if (idList == null || idList.size() != num) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_ID_FAIL);
        }
        return idList;
    }

    /**
     * 根据业务类型获取号段id
     *
     * @param bizType 业务类型，leaf系统分配的
     * @return
     */
    @CatAop
    @Override
    public String getSegmentID(String bizType) {
        String url = getUrl(properties.getPrefix(), bizType, MethodEnum.SEGMENT_GET);
        String id = HttpUtil.get(url, timeTime);
        if (StringUtils.isEmpty(id)) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_ID_FAIL);
        }
        return id;
    }

    /**
     * 根据业务类型批量获取号段id
     *
     * @param bizType 业务类型，leaf系统分配的
     * @param num     数量
     * @return
     */
    @CatAop
    @Override
    public List<String> getSegmentID(String bizType, int num) {
        String url = getUrl(properties.getPrefix(), bizType, MethodEnum.SEGMENT_BATCH_GET);
        if (num < 1 || num > 10000) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_NUM_FAIL);
        }
        url = url + "?num=" + num;
        String idsJson = HttpUtil.get(url, timeTime);
        JSONArray jsonArray = JSONObject.parseArray(idsJson);
        if(jsonArray == null){
            throw new EcpCommonException(LeafCodeEnum.PARAMS_ID_FAIL);
        }
        List<String> idList = jsonArray.toJavaList(String.class);
        if (idList == null || idList.size() != num) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_ID_FAIL);
        }
        return idList;
    }

    private String getUrl(String prefix, MethodEnum methodEnum) {
        return this.getUrl(prefix, defaultBizType, methodEnum);
    }

    private String getUrl(String prefix, String bizType, MethodEnum methodEnum) {
        if (StringUtils.isEmpty(prefix)) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_URL_FAIL);
        }
        if (StringUtils.isEmpty(bizType)) {
            throw new EcpCommonException(LeafCodeEnum.PARAMS_BIZ_TYPE_FAIL);
        }
        return prefix + methodEnum.getCodeMsg() + bizType;
    }

}
